/*
 * File:   main.c (Basestation)
 * Author: danecj
 *
 * Created on April 5, 2014, 1:04 PM
 */
/*
 * Potion Colors:
 *  Heal(1) - Red (255,0,0)
 *  Envenom(2) - Green (10,250,10)
 *  PowerUp(3) - Orange (255,128,0)
 *  Damage(4) - EXPLOSION
 *
 * Ingredent Colors:
 *  Water(1) - Light Blue (47,230,234)
 *  Dragon Scale(2) - Purple (128,0,128)
 *  Harpy Feather(3) - Yellow (255,255,0)
 *  Doxy Egg(4) - Light Yellow (255,255,55)
 *
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include "PICconfig.h"
#include "RGB_TLC5947.h"
#include "IR_Send.h"
#include "RFID.h"
#include "sound.h"

#define BUTTON PORTCbits.RC5


int loop, potion;

void setupPIC (void);
void delay(unsigned int cycles);
void reset_all(void);
void startLED(void);

/*
 *
 */
void main() {
    startTLC();
    all_off();
    //Setup PIC
    delay(30000);
    delay(30000);
    setupPIC();
    delay(30000);
    delay(30000);
    delay(30000);
    delay(30000);
    //Setup UART for RFID
    initRFID();
    //Start TLC5940 LED Driver
    startTLC();
    startLED();
    //Reset GS Array
    all_off();
    while(1){
        // Read tags and determine potion
        potion = readIngred();
        // Set potion type LED
        setLED_potion(potion);
        // Wait for button push to mix potion
        while(!BUTTON){};
        if(potion == 4){
            //Play sound indicating blowup
            playPotionSong(0003);
            blow_up();
        }
        if(potion != 4){
            //Set LED for potion
            writePotion(potion);
            delay(500);
            // Play sound indicating mixing of potion
            playPotionSong(0002);
            // Light LED for when potion vial can be placed
            place_potion();
            delay(30000);

            // Wait for button press to reset
            while(!BUTTON){};
            maxLED();
            writeLED();
            delay(10000);

        }

        reset_all();
    };
}

//All LEDs off
void reset_all(void){
    all_off();
}

//Set up PIC
void setupPIC (void) {
        //Set Int-HF oscillator @ 16Mhz
        OSCCONbits.IRCF0=1;
	OSCCONbits.IRCF1=1;
	OSCCONbits.IRCF2=1;

        //Wait for Frequency
        while(OSCCONbits.HFIOFS != 1);

        //Set port directions
        //A0-A3 Outputs
        TRISAbits.TRISA0 = 0;
        TRISAbits.TRISA1 = 0;
        TRISAbits.TRISA2 = 0;
        TRISAbits.TRISA3 = 0;
        //C2 Output
        TRISCbits.TRISC2 = 0;
        //C5 Input
        TRISCbits.TRISC5 = 1;
        soundINIT();

        //Set all low
        //A0-A3 Low
	LATAbits.LATA0 = 0;
        LATAbits.LATA1 = 0;
        LATAbits.LATA2 = 0;
        LATAbits.LATA3 = 0;
        //B1 Low
        LATBbits.LATB1 = 0;
}

//Delay Loop
void delay( unsigned int cycles){
    for(loop = 0; loop < cycles; loop++);
}

//LED Start Sequence
void startLED(void){

    fade_on(0,255,0,0,50);
    fade_on(1,255,0,0,50);
    fade_on(2,255,0,0,50);
    fade_on(6,255,0,0,50);
    fade_on(7,255,0,0,50);

    fade_on(0,0,255,0,50);
    fade_on(1,0,255,0,50);
    fade_on(2,0,255,0,50);
    fade_on(6,0,255,0,50);
    fade_on(7,0,255,0,50);

    fade_on(0,0,0,255,50);
    fade_on(1,0,0,255,50);
    fade_on(2,0,0,255,50);
    fade_on(6,0,0,255,50);
    fade_on(7,0,0,255,50);
    
}
